

def calculate_iou(xyxy, xyxy2):
    x1, y1, x2, y2 = xyxy
    _x1, _y1, _x2, _y2 = xyxy2
    xx = max(max(x1, _x1) - min(x2, _x2) + 1, 0)
    yy = max(max(y1, _y1) - min(y2, _y2) + 1, 0)
    I = xx * yy
    U = (x2 - x1 + 1) * (y2 - y1 + 1) + (_x2 - _x1 + 1) * (_y2 - _y1 + 1) - I
    iou = I / (U + 1e-20)
    return iou